﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Xml.Linq;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;

namespace LizardSoft.Common.FlexibleStructureDeployment
{
    public static partial class StructureHelper
    {
        private static void AddOrDeleteNeededFieldsInDefaultView(SPWeb web, string listFolderUrl, SPContentTypeId listContentTypeId)
        {
            SPList list = GetSPListFromFolderUrl(web, listFolderUrl);
            SPContentType listContentType = list.ContentTypes[listContentTypeId];
            SPView defaulView = list.DefaultView;
            foreach (SPField listContentTypeField in listContentType.Fields)
            {
                string internalName = listContentTypeField.InternalName;
                bool? showInViewForms = listContentTypeField.ShowInViewForms;
                if ((showInViewForms ?? false) && !defaulView.ViewFields.Exists(internalName))
                {
                    defaulView.ViewFields.Add(internalName);
                }
                else if ((!showInViewForms ?? true) && defaulView.ViewFields.Exists(internalName))
                {
                    defaulView.ViewFields.Delete(internalName);
                }
            }
            defaulView.Update();
        }
    }
}
